﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using POSSystem.DAL;
using System.Collections;
using System.Data.SqlClient;

namespace POSSystem.BLL
{
    public class SystemVariables :_SystemVariables
    {
        private static SystemVariables _sysVar;

        public static SystemVariables SysVars
        {
            get 
            {
                if (SystemVariables._sysVar == null)
                    SystemVariables._sysVar = new SystemVariables();
                return SystemVariables._sysVar; 
            }
        }

        public static void Reload()
        {
            _sysVar = new SystemVariables();
        }

        public SystemVariables()
        {
            this.LoadAll();
            if (variables == null)
            {
                variables = new Hashtable();
                for (int i = 0; i < RowCount; i++)
                {
                    string type = Type.ToLower();

                    if (type == "int")
                        variables[Name] = int.Parse(Value);
                    else if (type == "double")
                        variables[Name] = double.Parse(Value);
                    else if (type == "float")
                        variables[Name] = float.Parse(Value);
                    else if (type == "datetime")
                        variables[Name] = DateTime.Parse(Value);
                    else if (type == "string")
                        variables[Name] = Value;
                    else if (type == "decimal")
                        variables[Name] = Decimal.Parse(Value);

                    MoveNext();
                }
            }


            if (connectionStringBuilder == null)
            {
                connectionStringBuilder = new SqlConnectionStringBuilder( Utilities.GetConfigAppSetting(this.ConnectionStringConfig));
            }
        }

        static Hashtable variables;

        public Decimal GST
        {
            get { return (Decimal)variables["GST"]; }
            set { variables["GST"] = value; }
        }

        public int IMEILength
        {
            get { return (int)variables["IMEILength"]; }
            set { variables["IMEILength"] = value; }
        }

        public int TimeZone
        {
            get { return (int)variables["TimeZone"]; }
            set { variables["TimeZone"] = value; }
        }

        public string CompanyName
        {
            get { return variables["CompanyName"].ToString(); }
            set { variables["CompanyName"] = value; }
        }

        public Decimal ShippingCost
        {
            get { return (Decimal)variables["ShippingCost"]; }
            set { variables["ShippingCost"] = value; }
        }

        public string CompanyAddress
        {
            get { return variables["CompanyAddress"].ToString(); }
            set { variables["CompanyAddress"] = value; }
        }

        public string CompanyABN
        {
            get { return variables["CompanyABN"].ToString(); }
            set { variables["CompanyABN"] = value; }
        }

        public string CompanyPhone
        {
            get { return variables["CompanyPhone"].ToString(); }
            set { variables["CompanyPhone"] = value; }
        }

        public string CompanyFax
        {
            get { return variables["CompanyFax"].ToString(); }
            set { variables["CompanyFax"] = value; }
        }

        public string InvoiceFooter
        {
            get { return variables["InvoiceFooter"].ToString(); }
            set { variables["InvoiceFooter"] = value; }
        }

        public string PurchaseFooter
        {
            get { return variables["PurchaseFooter"].ToString(); }
            set { variables["PurchaseFooter"] = value; }
        }

        public string RepairFooter
        {
            get { return variables["RepairFooter"].ToString(); }
            set { variables["RepairFooter"] = value; }
        }

        public string RepairSubHeader
        {
            get { return variables["RepairSubHeader"].ToString(); }
            set { variables["RepairSubHeader"] = value; }
        }

        public string DateFormat
        {
            get { return variables["DateFormat"].ToString(); }
            set { variables["DateFormat"] = value; }
        }

        public string ResetPasswordEmailTemplate
        {
            get { return variables["ResetPasswordEmailTemplate"].ToString(); }
            set { variables["ResetPasswordEmailTemplate"] = value; }
        }

        public int CashInvoiceType
        {
            get { return (int)variables["CashInvoiceType"]; }
            set { variables["CashInvoiceType"] = value; }
        }

        static SqlConnectionStringBuilder connectionStringBuilder;

        public string Server
        {
            get { return connectionStringBuilder.DataSource; }
        }

        public string Database
        {
            get { return connectionStringBuilder.InitialCatalog; }
        }

        public string UserID
        {
            get { return connectionStringBuilder.UserID; }
        }

        public string Password
        {
            get { return connectionStringBuilder.Password; }
        }
    }
}
